package vip.aster.workflow.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import vip.aster.common.utils.PageInfo;
import vip.aster.common.utils.ResultInfo;
import vip.aster.framework.log.annotation.Log;
import vip.aster.framework.log.enums.BusinessTypeEnum;
import vip.aster.workflow.entity.WfForm;
import vip.aster.workflow.query.WfFormQuery;
import vip.aster.workflow.service.ProcessService;
import vip.aster.workflow.service.WfFormService;
import vip.aster.workflow.vo.WfFormVO;

import java.util.List;

/**
 * 流程表单管理
 *
 * @author Aster lipian1004@163.com
 * @since 2024-05-11 09:48:46
 */
@Tag(name = "流程表单管理")
@RestController
@RequestMapping("/workflow/form")
@AllArgsConstructor
public class WfFormController {

    /**
     * 流程表单service
     */
    private final WfFormService wfFormService;

    /**
     * 流程service
     */
    private final ProcessService processService;

    @GetMapping("/page")
    @Operation(summary = "分页查询表单")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:form:list')")
    public ResultInfo<PageInfo<WfFormVO>> page(@ParameterObject @Valid WfFormQuery query) {
        PageInfo<WfFormVO> page = wfFormService.pageList(query);
        return ResultInfo.success(page);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询表单")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:form:list')")
    public ResultInfo<List<WfFormVO>> list(@ParameterObject @Valid WfFormQuery query) {
        List<WfFormVO> list = wfFormService.list(query);
        return ResultInfo.success(list);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "详情")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:form:info')")
    public ResultInfo<WfFormVO> get(@PathVariable("id") String id) {
        WfForm entity = wfFormService.getById(id);
        return ResultInfo.success(new WfFormVO(entity));
    }

    @GetMapping("/code/{code}")
    @Operation(summary = "根据CODE查询最新一条表单信息")
    @Log(type = BusinessTypeEnum.SEARCH)
    @PreAuthorize("hasAuthority('wf:form:info')")
    public ResultInfo<WfFormVO> getFormByCode(@PathVariable("code") String code) {
        WfForm entity = wfFormService.getLastFormInfo(code);
        return ResultInfo.success(new WfFormVO(entity));
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    @Log(type = BusinessTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('wf:form:add','wf:form:edit')")
    public ResultInfo<String> save(@RequestBody WfFormVO vo) {
        wfFormService.save(vo);
        return ResultInfo.success();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @Log(type = BusinessTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('wf:form:delete')")
    public ResultInfo<String> delete(@RequestBody List<String> idList) {
        wfFormService.wfFormDelete(idList);
        return ResultInfo.success();
    }

    @PostMapping("/deployment/{id}")
    @Operation(summary = "部署")
    @Log(type = BusinessTypeEnum.UPDATE)
    public ResultInfo<String> deployment(@PathVariable String id) {
        System.out.println("部署：" + id);
        WfForm wfForm = wfFormService.getById(id);
        processService.deployment(wfForm);
        return ResultInfo.success();
    }


}
